/**
 * 
 */
package org.markwebb.mina.playground.udp.server;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.concurrent.Executors;

import org.apache.mina.common.IdleStatus;
import org.apache.mina.common.IoAcceptor;
import org.apache.mina.common.IoHandlerAdapter;
import org.apache.mina.common.IoSession;
import org.apache.mina.transport.socket.nio.DatagramAcceptor;

/**
 * 
 * Date: Apr 25, 2007
 * @author mark
 */
public class UDPServer extends IoHandlerAdapter {

	public static final int PORT = 9000;
	
	public UDPServer() throws IOException{
		
		IoAcceptor acceptor = new DatagramAcceptor();
		acceptor.setLocalAddress( new InetSocketAddress(PORT));
		acceptor.setHandler( this );
		acceptor.bind();
		System.out.println("UDPServer listening on port " + PORT);
	}

	@Override
	public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
		cause.printStackTrace();
		session.close();
	}

	@Override
	public void messageReceived(IoSession session, Object message) throws Exception {
		System.out.println( "Received " + message );
	}

	@Override
	public void sessionClosed(IoSession session) throws Exception {
		System.out.println("Session closed...");
	}

	@Override
	public void sessionCreated(IoSession session) throws Exception {
		System.out.println("Session created...");
	}

	@Override
	public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
		System.out.println("Session idle...");
	}

	@Override
	public void sessionOpened(IoSession session) throws Exception {
		System.out.println("Session Opened...");
	}

	public static void main(String[] args) throws IOException {
		new UDPServer();
	}
}
